package com.thesis.object;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * ListDishType class
 * 
 * save all dish types
 * 
 * 
 * @author NgocTuyen
 *
 */

public class ListDishType {
	//instance variable
	private static ListDishType instance = new ListDishType();
	
	//array list save all dish type
	private ArrayList<DishType> listDishType;
	
	/**
	 * Constructor function
	 * 
	 * Constructor
	 * 
	 * @access private 
	 * 
	 * @param null
	 * 
	 * @return null
	 * 
	 */
	private ListDishType(){
		listDishType = new ArrayList<DishType>();
	}
	
	/**
	 * getInstance static function
	 * 
	 * @access public
	 * 
	 * @param null
	 * 
	 * @return ListDishType
	 *  + instance object
	 */
	public static ListDishType getInstance(){
		if (instance == null){
			instance = new ListDishType();
		}
		return instance;
	}
	
	/**
	 * addDishTypeByJson function
	 * 
	 * add all dishe types into list dishe types
	 * 
	 * @access public
	 * 
	 * @param listJson: list of all dishe types
	 * 
	 * $return void
	 */
	public void addDishTypeByJson(JSONArray listJson){
		for (int i = 0; i < listJson.length(); i++) {
			try {
				addDishType(listJson.getJSONObject(i));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * addDishType function
	 * 
	 * add a dish type in to list dishe types
	 * 
	 * @access private
	 * 
	 * @param jsonObj: json of one dish type
	 * 
	 * @return void
	 */
	public void addDishType(JSONObject json){
		try {
			DishType dishType = new DishType(json.getInt("_id"), json.getString("name"));
			listDishType.add(dishType);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * getListDishTypes function
	 * 
	 * get list all dishes types in restaurant
	 * 
	 * @access public
	 * 
	 * @param null
	 * 
	 * @return ArrayList<DishType>
	 *  + list of all dishe types
	 */
	public ArrayList<DishType> getListDishTypes(){
		return listDishType;
	}
	
	public void clear()  
	 {  
	   instance = null;  
	 }  

}
